package cn.szzsi.common.kit;


import cn.szzsi.common.bean.dto.*;
import cn.szzsi.common.model.WeixinMenu;
import com.jfinal.kit.JsonKit;

import java.util.List;


public class WeixinMenuUtil {

	/**
	 * 		封装菜单数据
	 * */
	public static String getMenu(String uid){
		List<WeixinMenu> pmenuList= WeixinMenu.getPmenuList(uid);
		Object object[]=new Object[pmenuList.size()];
		for(int i=0;i<pmenuList.size();i++){
			String pid=pmenuList.get(i).getStr("id");
			String pmenu_name=pmenuList.get(i).getStr("main_menu");//一级菜单名称
			String pKeyWordOrUrl=pmenuList.get(i).getStr("keyword_or_url");//关键词或者链接
			//根据一级菜单查询二级菜单
			List<WeixinMenu> zmunuList= WeixinMenu.getZmenuList(uid,pid);
			if(zmunuList.size()==0){//没有二级菜单的情况下
				WeixinCommonButton cb = new WeixinCommonButton();//二级菜单click型
				WeixinViewButton vb=new WeixinViewButton();//二级菜单view型
				WeixinButton button[]=new WeixinButton[pmenuList.size()];//定义一个Button类型的数组
				if(pKeyWordOrUrl.indexOf("http://")!=-1 || pKeyWordOrUrl.indexOf("https://")!=-1){//网址(view)
					vb.setUrl(pKeyWordOrUrl);
					vb.setName(pmenu_name);
					vb.setType("view");
					button[i]=vb;
					object[i]=vb;
				}else{//关键字(key)
					cb.setKey(pKeyWordOrUrl);
					cb.setName(pmenu_name);
					cb.setType("click");
					button[i]=cb;
					object[i]=cb;
				}
			}else{//含有二级菜单的情况下
				WeixinComplexButton cx=new WeixinComplexButton();
				WeixinButton button[]=new WeixinButton[zmunuList.size()];//定义一个Button类型的数组
				for(int j=0;j<zmunuList.size();j++){
					WeixinCommonButton cb = new WeixinCommonButton();//二级菜单click型
					WeixinViewButton vb=new WeixinViewButton();//二级菜单view型
					String zmenu_name=zmunuList.get(j).getStr("main_menu");//二级菜单名称
					String keyWordOrUrl=zmunuList.get(j).getStr("keyword_or_url");//关键词或者链接
					//判断是关键字还是链接
					if(keyWordOrUrl.indexOf("https://")!=-1 || keyWordOrUrl.indexOf("http://")!=-1){//网址(view)
						vb.setUrl(keyWordOrUrl);
						vb.setName(zmenu_name);
						vb.setType("view");
						button[j]=vb;
					}else{//关键字(key)
						cb.setKey(keyWordOrUrl);
						cb.setName(zmenu_name);
						cb.setType("click");
						button[j]=cb;
					}
				}
				cx.setName(pmenu_name);//主菜单名称
				cx.setSub_button(button);//主菜单下面的主菜单按钮
				object[i]=cx;
			}
		}
		WeixinObjectMenu objectMenu=new WeixinObjectMenu();
		objectMenu.setButton(object);
		String menu_str= JsonKit.toJson(objectMenu);
		return menu_str;
	}

}

